/**
 ********************************************************************************
 *
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：
 *
 *修改历史：
 *
 ********************************************************************************
 ***/

/*******************************	头文件包含区	**********************************/
#include "at_cmd_rx.h"
#include "module_control.h"

/******************************* 全局变量定义区 **********************************/
mainUartStryct mainUart __attribute__((at(0x10000400)));
extern uint8_t MD5_Check(uint32_t start_addr, uint32_t len);
#if (PROJECT_TYPEDEF == NB)
uint16_t count_ota = 1;
#elif (PROJECT_TYPEDEF == NB_ONENET)
static uint16_t ota_package_addr = 0x26ff;
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void str_to_hex(char *str, uint16_t strLen, uint8_t *outBuf)
{
	uint16_t i;
	uint8_t bufH = 0, bufL = 0;
	for (i = 0; i < strLen; i += 2)
	{
		if (str[i] <= '9')
			bufH = (str[i] - '0') << 4;
		else
			bufH = (str[i] - 55) << 4;

		if ((i + 1) < strLen)
		{
			if (str[i + 1] <= '9')
				bufL = (str[i + 1] - '0');
			else
				bufL = (str[i + 1] - 55);
		}
		else
		{
			bufL = 0;
		}
		outBuf[i / 2] = bufH | bufL;
	}
}

#if (PROJECT_TYPEDEF == CAT1)
void AT_RX_UPLINK(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)">") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：void AT_RX_Restart(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：模块重启信息返回判断
 *
 *********************************************************************************
 **/
void AT_RX_Restart(void)
{
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
	if ((strstr((char *)mainUart.my_at_readbuf, (char *)"Neul") != NULL) || 
		(strstr((char *)mainUart.my_at_readbuf, (char *)"REBOOT") != NULL))
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"RDY") != NULL)
#endif
	{
		// go_flag = 1;
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		reg.R.wireless_run_state.start = 1;
	}
}
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
static uint8_t NB_return_error_typedef(void)
{
	uint8_t error_type;
	if (strstr((const char *)mainUart.my_at_readbuf, "+QLWEVTIND:") != NULL)
	{
		error_type = 1;
	}
	else
	{
		error_type = 0;
	}
	return error_type;
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：void AT_RX_Universal(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：通用的AT指令接收处理函数
 *
 *********************************************************************************
 **/
void AT_RX_Universal(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		if (strstr((char *)mainUart.my_at_readbuf, (char *)"BC28-F") != NULL)
		{
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_QSCLK, AT_RX_Universal, 10, 2000, ACK);
		}
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
#if (PROJECT_TYPEDEF == NB)
	else if (NULL != NB_return_error_typedef())
	{
	}
#endif
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"ERROR") != NULL)
	{
		global_control.error_code = global_control.cmd_num;
		
	}
}

#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)

void AT_RX_NMSTATUS(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"MO_DATA_ENABLED") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;

		device_info.mqtt_state = NB_LWM2M_OK;
		sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
		// update_lastTimeAndNextTime();
		device_info.QmtSubFlag = 1;

		device_time.first_data = 0;
		device_time.lamp_data_report_time = 0;
		device_time.else_data_repota_time =  ((u32)reg.W.data_tx_time * 100) + device_info.random - 6000;
		device_time.at_time = 30000 * 12 - 6000;
#if (PROJECT_TYPEDEF == NB)
		if (reg.RW.ota_flag == OTA_PACKAGE_RX_OK)
		{
			mainUart.my_data_buf[3] = 23;
			my_cmd_deal(DOWMLINK_READ, APP_PACKsend_start_ota_23, waiting_to_receive, 50, 2000, NO_ACK);

			mainUart.my_data_buf[3] = 24;

			my_cmd_deal(DOWMLINK_READ, APP_PACKsend_report_ota_result_24, waiting_to_receive, 50, 2000, NO_ACK);
			reg.RW.ota_flag = NONE_OTA;
			data_flash_write();
		}
#endif
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"DEREGISTERED") != NULL)
	{
		// NETWORKLOG |= (1<<0);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"INIT_FAILED") != NULL)
	{
		// NETWORKLOG |= (1<<1);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"REJECTED_BY_SERVER") != NULL)
	{
		// NETWORKLOG |= (1<<2);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"TIMEOUT_AND_RETRYING") != NULL)
	{
		//	NETWORKLOG |= (1<<3);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"REG_FAILED") != NULL)
	{
		// NETWORKLOG |= (1<<4);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"NO_UE_IP") != NULL)
	{
		// NETWORKLOG |= (1<<5);
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
#if (PROJECT_TYPEDEF == NB)
	else if (NULL != NB_return_error_typedef())
	{
	}
#endif
	else
	{
		device_info.mqtt_state = NB_LWM2M_CFG;
		global_control.error_code = global_control.cmd_num;
	}
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_QCCID(void)
{
#if (PROJECT_TYPEDEF == NB)
	if (strstr((const char *)mainUart.my_at_readbuf, "+NCCID:") != NULL && m_uart1.rx_len == 37)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((const char *)mainUart.my_at_readbuf, "+NCCID:") != NULL && m_uart1.rx_len == 37)
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((const char *)mainUart.my_at_readbuf, "+QCCID:") != NULL && m_uart1.rx_len == 38)
#endif
	{
#if (PROJECT_TYPEDEF == NB)
		memmove(device_info.iccid, &mainUart.my_at_readbuf[9], 20);
#elif (PROJECT_TYPEDEF == NB_ONENET)
		memmove(device_info.iccid, &mainUart.my_at_readbuf[9], 20);
#elif (PROJECT_TYPEDEF == CAT1)
		memmove(device_info.iccid, &mainUart.my_at_readbuf[10], 20);
#endif
		str_to_hex((char *)device_info.iccid, 20, reg.R.iccid);
		swapUint16_t_Plus((u16 *)reg.R.iccid, 5);

		//LOGD("device_info.iccid:%s", device_info.iccid);
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		reg.R.wireless_run_state.iccid = 1;
	}
#if (PROJECT_TYPEDEF == NB)
	else if (NULL != NB_return_error_typedef())
	{
	}
#endif
#if (PROJECT_TYPEDEF == NB)
	if (strstr((const char *)mainUart.my_at_readbuf, "+NCCID:") != NULL && m_uart1.rx_len != 37)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((const char *)mainUart.my_at_readbuf, "+NCCID:") != NULL && m_uart1.rx_len != 37)
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((const char *)mainUart.my_at_readbuf, "+QCCID:") != NULL && m_uart1.rx_len != 38)
#endif
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.iccid = 0;
	}

	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"ERROR") != NULL)
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.iccid = 0;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_IMEI(void)
{
	if (strstr((const char *)mainUart.my_at_readbuf, "OK\r\n") != NULL)
	{
#if (PROJECT_TYPEDEF == NB)
		memcpy(device_info.imei, mainUart.my_at_readbuf + 8, 15);
#elif (PROJECT_TYPEDEF == NB_ONENET)
		memcpy(device_info.imei, mainUart.my_at_readbuf + 8, 15);
#elif (PROJECT_TYPEDEF == CAT1)
		memcpy(device_info.imei, mainUart.my_at_readbuf + 2, 15);
#endif
		//LOGD("device_info.imei:%s\r\n", device_info.imei);
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		reg.R.wireless_run_state.imei = 1;
		memset(reg.R.module_id, 0, sizeof(reg.R.module_id));
		str_to_hex((char *)device_info.imei, 15, reg.R.module_id);
		swapUint16_t_Plus((u16 *)reg.R.module_id, 5);
	}
#if (PROJECT_TYPEDEF == NB)
	else if (NULL != NB_return_error_typedef())
	{
	}
#endif
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"ERROR") != NULL)
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.imei = 0;
		// LOGD("error_code:%d",global_control.error_code);
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_CREG(void)
{
#if (PROJECT_TYPEDEF == NB)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CEREG:0,1") != NULL)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CEREG:0,1") != NULL)
#else
	if (strstr((const char *)mainUart.my_at_readbuf, "+CREG: 0,1") != NULL)
#endif

	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		reg.R.wireless_run_state.creg = 1;
	}
	else
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.creg = 0;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_CGATT(void)
{
#if (PROJECT_TYPEDEF == NB)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CGATT:1") != NULL)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CGATT:1") != NULL)
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CGATT: 1") != NULL)
#endif
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		reg.R.wireless_run_state.cgatt = 1;
#if (PROJECT_TYPEDEF == NB)
		// device_info.mqtt_state = NB_LWM2M_CFG;
#endif
	}
#if (PROJECT_TYPEDEF == NB)
	else if (NULL != NB_return_error_typedef())
	{
	}
#endif

	else
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.cgatt = 0;
#if (PROJECT_TYPEDEF == NB)
		device_info.mqtt_state = NB_IN_INITCFG;
#endif
	}
}

/**
 *********************************************************************************
 *
 *函数名称：void AT_RX_CSQ(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：查询信号值
 *
 *********************************************************************************
 **/
#if 0
void AT_RX_CSQ(void)
{
	uint16_t csq;
#if (PROJECT_TYPEDEF == NB)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CSQ:") != NULL && m_uart1.rx_len == 20)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CSQ:") != NULL && m_uart1.rx_len == 20)
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((const char *)mainUart.my_at_readbuf, "+CSQ:") != NULL && m_uart1.rx_len == 21)
#endif
	{
#if (PROJECT_TYPEDEF == NB)
		csq = (mainUart.my_at_readbuf[7] - 0x30) * 10 + (mainUart.my_at_readbuf[8] - 0x30);
#elif (PROJECT_TYPEDEF == NB_ONENET)
		csq = (mainUart.my_at_readbuf[7] - 0x30) * 10 + (mainUart.my_at_readbuf[8] - 0x30);
#elif (PROJECT_TYPEDEF == CAT1)
		csq = (mainUart.my_at_readbuf[8] - 0x30) * 10 + (mainUart.my_at_readbuf[9] - 0x30);
#endif

#if (PROJECT_TYPEDEF == NB)
		if (csq != 99 && csq >= 8)
#elif (PROJECT_TYPEDEF == NB_ONENET)
		if (csq != 99 && csq >= 8)
#else
		if (csq != 99 && csq != 0)
#endif
		{
			device_info.csq = csq;
			reg.R.csq = csq;
			global_control.ack_ok = 1;
			global_control.error_code = 0;
			reg.R.wireless_run_state.csq = 1;
		}
#if (PROJECT_TYPEDEF == NB)
		else if (NULL != NB_return_error_typedef())
		{
		}

#endif
		else
		{
			global_control.error_code = global_control.cmd_num;
			reg.R.wireless_run_state.csq = 0;
		}
	}
	else
	{
		global_control.error_code = global_control.cmd_num;
		reg.R.wireless_run_state.csq = 0;
	}
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
#if 0
void AT_RX_CCLK(void)
{
	char *StrHead = NULL;
	uint8_t year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0;
#if (PROJECT_TYPEDEF == NB)
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#else
#ifdef CAT1_NETWORK_TIME
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QNTP:") != NULL)
#else
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#endif
#endif
	{
#if (PROJECT_TYPEDEF == NB)
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+CCLK:");
#elif (PROJECT_TYPEDEF == NB_ONENET)
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+CCLK:");
#else
#ifdef CAT1_NETWORK_TIME
        if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QNTP: 569") != NULL)
		{
			get_cclk_another_way();
		}
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+QNTP:");
#else
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+CCLK:");
#endif
#endif

#if (PROJECT_TYPEDEF == NB)
		StrHead = StrHead + 6;
#elif (PROJECT_TYPEDEF == NB_ONENET)
		StrHead = StrHead + 6;
#else
#ifdef CAT1_NETWORK_TIME
		StrHead = StrHead + 12;
#else
		StrHead = StrHead + 8;
#endif
#endif

		year = (StrHead[0] - '0') * 10 + StrHead[1] - '0';
		month = (StrHead[3] - '0') * 10 + StrHead[4] - '0';
		day = (StrHead[6] - '0') * 10 + StrHead[7] - '0';
		hour = (StrHead[9] - '0') * 10 + StrHead[10] - '0';
		min = (StrHead[12] - '0') * 10 + StrHead[13] - '0';
		sec = (StrHead[15] - '0') * 10 + StrHead[16] - '0';
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		if (year >= 21 && year <= 50) // 30年
		{
			srand(min + sec);					//播种
			device_info.random = rand() % 6000; //生成0~60000的随机数
			//LOGD("device_info.random:%d\r\n", device_info.random);

			set_rtc_time(year, month, day, hour, min, sec);
			get_sunset_rise_time();
			if(timestampTable[compareStruct.nextSceneSeek] != TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec))
			{
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			}
		}
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		return;
	}
	else
	{
		global_control.error_code = global_control.cmd_num;
	}
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_QMTOPEN(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTOPEN:") != NULL && m_uart1.rx_len == 17)
	{
		if (mainUart.my_at_readbuf[14] == '0')
		{
			global_control.ack_ok = 1;
			global_control.error_code = 0;
		}
		else
		{
			global_control.error_code = global_control.cmd_num;
			
		}
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL) //不判断OK
	{
		return;
	}
	else
	{
		global_control.error_code = global_control.cmd_num;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void AT_RX_QMTCONN(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTCONN:") != NULL && m_uart1.rx_len == 19)
	{
		if (mainUart.my_at_readbuf[14] == '0')
		{
			global_control.ack_ok = 1;
			global_control.error_code = 0;
		}
		else
		{
			global_control.error_code = global_control.cmd_num;
			
		}
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL) //不判断OK
	{
		return;
	}
	else
	{
		
		global_control.error_code = global_control.cmd_num;
	}
}
#if (PROJECT_TYPEDEF == CAT1)
static char *Find_Pos(char *data, char c, int pos)
{
    char *p = data;
    int cnt = 0;
    for(;*p!=NULL;p++)//数据索引
    {
        if(*p == c)//判断数据与给定数据
        {
            cnt++;
            if(cnt == pos)//第几个
            {
                return p;
            }
        }
    }
    return NULL;
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
#if (PROJECT_TYPEDEF == CAT1)
void AT_RX_QMTSUB(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTSUB:") != NULL && m_uart1.rx_len == 20) //数据上报返回
	{
		if (mainUart.my_at_readbuf[15] == '0')
		{

#if (PROJECT_TYPEDEF == NB)
			device_info.mqtt_state = NB_UINIT;
#elif (PROJECT_TYPEDEF == CAT1)
			device_info.mqtt_state = 1;
#endif

			global_control.ack_ok = 1;
			global_control.error_code = 0;
			device_info.QmtSubFlag = 1;
			device_time.first_data = 0;
			device_time.lamp_data_report_time = 0;
			device_time.else_data_repota_time = ((u32)reg.W.data_tx_time * 100) + device_info.random - 6000;
			device_time.at_time = 30000 * 12 - 6000;
			reg.R.wireless_run_state.uplink = 1;
		}
		else
		{
			global_control.error_code = global_control.cmd_num;
			
			reg.R.wireless_run_state.uplink = 0;
		}
	}
}
#endif
extern u16 ota_checkBuf;
#if (PROJECT_TYPEDEF == NB)
BOOL start_ota_flag = false;
void ota_Timeout_handle(void)
{
	static uint16_t count_report_ota = 0, count_time_ota = 0;
	static uint8_t flag = 0;
	// MY_QUEUE queue;
	if (reg.RW.ota_flag == OTA_READY) //&& device_info.mqtt_state == NB_LWM2M_OK)
	{
		if (count_ota == 0)
		{
			flag = 1;
			count_time_ota = 0;
			count_report_ota = 0;
		}
		if (flag == 1)
		{
			count_report_ota++;
			if (count_report_ota >= 100 * 60 * 2)
			{
				count_report_ota = 0;
				my_cmd_deal(DOWMLINK_READ, AEP_PackSend, waiting_to_receive, 1, 2000, ACK);
			}
		}
		count_ota++;
		if (count_ota >= 100 * 60 * 1)
		{
			count_ota = 1;
			count_time_ota++;
			if (count_time_ota >= 5)
			{
				count_time_ota = 0;
				reg.RW.ota_flag = NONE_OTA;
				device_info.mqtt_state = NB_LWM2M_OK;
			}
		}
	}
}

uint16_t whole_Pakage = 0;
extern uint16_t current_ota_serial;
extern uint8_t OTA_Result_flag;
void AEP_OTA_Process(uint8_t *buf_hex)
{
	uint16_t CRC_RAW;
	uint16_t CRC_data;

	if (buf_hex[3] == 19) // 46
	{
		memset(&device_time, 0, sizeof(device_time));
		my_cmd_deal(DOWMLINK_READ, APP_PACKsend_inquiry_vereion_19, waiting_to_receive, 50, 2000, NO_ACK);
		current_ota_serial = 0;
		reg.W.software_len = 0;
		reg.W.ota_sum_len = 0;
		memset(&device_time, 0, sizeof(device_time));
	}
	else if (buf_hex[3] == 20) // 47
	{
		reg.W.software_len = (buf_hex[26] << 8) + buf_hex[27];

		if (buf_hex[9] && buf_hex[10] && buf_hex[11] && buf_hex[12] && buf_hex[13] && (!buf_hex[14]) && (!buf_hex[15]))
		{
			start_ota_flag = TRUE;
			reg.RW.OTA_new_version = (buf_hex[9] - '0') * 10000 + (buf_hex[10] - '0') * 1000 + (buf_hex[11] - '0') * 100 + (buf_hex[12] - '0') * 10 + (buf_hex[13] - '0');
			OTA_Result_flag = 0;
			//LOGD("start_ota_flag : %d\r\n", start_ota_flag);
			 ota_checkBuf = 0x26ff;
		}
		else
		{
			OTA_Result_flag = 0x03;
			//LOGD("version error!!!\r\n");
		}
		current_ota_serial = 0;
		my_cmd_deal(DOWMLINK_READ, APP_PACKsend_vereion_notify_20, waiting_to_receive, 50, 2000, NO_ACK);
		memset(&device_time, 0, sizeof(device_time));
	}
	else if (buf_hex[3] == 21) // 47
	{
		uint16_t num2;
		// uint32_t mydata;
		memset(&device_time, 0, sizeof(device_time));
		num2 = (buf_hex[6] << 8) + buf_hex[7] - 3;
		CRC_RAW = (buf_hex[4] << 8) + buf_hex[5];
		buf_hex[4] = 0;
		buf_hex[5] = 0;
		CRC_data = do_crc(0x0000, buf_hex, num2 + 11);
		if (CRC_RAW == CRC_data && buf_hex[8] != 0x81)
		{
			protocol.reg_addr[0] = 0x2700 + current_ota_serial;
			current_ota_serial = (buf_hex[9] << 8) + buf_hex[10] + 1;
			buf_hex += 11;
			protocol.reg_len[0] = num2 / 2;
			protocol.data_buf[0] = buf_hex;
			/*将数据写入到内存中，同时对地址判断是场景策略包还是升级包还是其他正常包*/
			protocol_write(protocol.reg_addr[0], protocol.data_buf[0], protocol.reg_len[0]);

			if (current_ota_serial == reg.W.software_len)
			{
				//LOGD("COMPLETE!!!\r\n");

				//LOGD("OTA length:%d", reg.W.ota_sum_len);
				otaState_check(1);

				reg.W.ota_sum_len = reg.W.ota_sum_len - 16;
				if (MD5_Check(FLASH_OTA_ADDR, reg.W.ota_sum_len)) // MD5计算
				{
					LOGD("MD5 ok");
					reg.RW.ota_flag = OTA_PACKAGE_RX_OK;
					mainUart.my_data_buf[3] = 22;
					OTA_Result_flag = 0;
					reg.R.upgrade_mark = 0;
					my_cmd_deal(DOWMLINK_READ, APP_PACKsend_report_ota_status_22, waiting_to_receive, 50, 2000, NO_ACK);
				}
				else
				{
					LOGD("MD5 fail");
					reg.RW.ota_flag = OTA_FAIL;
					device_info.mqtt_state = NB_LWM2M_OK;
					mainUart.my_data_buf[3] = 22;
					OTA_Result_flag = 0x07;
					my_cmd_deal(DOWMLINK_READ, APP_PACKsend_report_ota_status_22, waiting_to_receive, 50, 2000, NO_ACK);
				}
			}
			else //if(reg.RW.ota_flag == OTA_READY)
			{
				my_cmd_deal(DOWMLINK_READ, APP_PACKsend_request_ota_pack_21, waiting_to_receive, 50, 2000, NO_ACK);
				memset(&device_time, 0, sizeof(device_time));
				count_ota = 0;
			}
		}
		else
		{
			count_ota = 0;
		}
	}
	else if (buf_hex[3] == 22) // 49
	{
		memset(&device_time, 0, sizeof(device_time));
	}
	else if (buf_hex[3] == 23)
	{
		memset(&device_time, 0, sizeof(device_time));
		data_flash_write();
		IAP_APP_Jump();
	}
	else if (buf_hex[3] == 24)
	{
		memset(&device_time, 0, sizeof(device_time));
	}
}
#endif
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
void AT_RX_SWITCH_BAUD(void)
{
	if (strstr((const char *)mainUart.my_at_readbuf, "OK") != NULL)
	{
		uart_switch_baud(USART_BPS_115200);
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
}

void AT_RX_CEDRXS(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		reg.RW.reset_process = 16;
		data_flash_write();
		LOGD("AT_RX_CEDRXS\r\n");
		my_cmd_init();
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
	else if (NULL != NB_return_error_typedef())
	{
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"ERROR") != NULL)
	{
		global_control.error_code = global_control.cmd_num;
	}
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
#if (PROJECT_TYPEDEF == NB_ONENET)
uint32_t ONEent_msgld = 0;
void delay_MIPLDISCOVER(void)
{
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLDISCOVER:") != NULL) //数据接收
	{
		char *p = NULL;
		p = strstr((char *)mainUart.my_at_readbuf, (char *)",");
		ONEent_msgld = atoi(&p[1]);

		device_info.mqtt_state = NB_LWM2M_OK;
		sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
		device_info.QmtSubFlag = 1;

		device_time.first_data = 0;
		device_time.lamp_data_report_time = 0;
		device_time.else_data_repota_time =  ((u32)reg.W.data_tx_time * 100) + device_info.random - 6000;
		device_time.at_time = 30000 * 12 - 6000;
		// my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 2000, NO_ACK); //查询卡的状态，同时转为接收模式等待接
		my_cmd_deal(AT_QMTCFG_3, AT_TX_QMTCFG_3, waiting_to_receive, 10, 3000, ACK);
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
}
#endif
extern u8 led1RestartFlag; //刚上电时触发渐变开关灯效果标志




#if (PROJECT_TYPEDEF == CAT1)
uint32_t timestampCompare[TIMESCOMPARE];
extern uint8_t get_cclk_style ;
uint8_t Prase_time(void)
{
	int i = 0;
	for(i = 0; i< get_cclk_style;i++ )
	{
		if((abs(timestampCompare[get_cclk_style] - timestampCompare[i]) <= 30 * 6) && ((timestampCompare[get_cclk_style] > 1577808000) && (timestampCompare[get_cclk_style] < 4070880000)))
		{
			return 0;
		}
	}
	get_cclk_another_way();
	return 1;	
}

#endif


void waiting_to_receive(void)
{
	char *str_start_addr = NULL, *str_end_addr = NULL;
	volatile uint16_t str_len, i;
	char *str_addr = NULL;
	char *StrHead = NULL;
	uint16_t csq;
	uint8_t year, month, day, hour, min, sec;
#if (PROJECT_TYPEDEF == NB)
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+NNMI:") != NULL) //数据接收
#elif (PROJECT_TYPEDEF == NB_ONENET)
	AT_RX_Universal();

	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLWRITE:") != NULL) //数据接收
#elif (PROJECT_TYPEDEF == CAT1)
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTPUBEX:") != NULL )
	{
		StrHead = strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTPUBEX:");
		if (StrHead[15] == '0')
		{
			global_control.ack_ok = 1;
			global_control.error_code = 0;
			if(leakDete.send_lamp1_time_flag == 3)
			{
				leakDete.send_lamp1_time_flag = 0;
				reg.W.lamp1_power_off_timestamp = 0;
				reg.W.lamp1_power_on_timestamp = 0;
				LOGD("lamp_1_data send over\r\n");

			}
			if(leakDete.send_lamp2_time_flag == 3)
			{
				reg.W.lamp2_power_off_timestamp = 0;
				reg.W.lamp2_power_on_timestamp = 0;
				leakDete.send_lamp2_time_flag = 0;
				LOGD("lamp_1_data send over\r\n");
			}
		}
		else
		{
			global_control.error_code = global_control.cmd_num;
			
		}
	}
	
	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTRECV:") != NULL) //数据接收
#endif
	{
		str_addr = (char *)mainUart.my_at_readbuf;
#if (PROJECT_TYPEDEF == NB)
		if (strstr((char *)str_addr, (char *)",FFFE") != NULL)
		{
			str_start_addr = strstr((char *)str_addr, ",");
			if (str_start_addr == NULL)
			{
				return;
			}
			str_start_addr = str_start_addr + 1;
			if (strstr((char *)(str_start_addr + 2), "\r\n") == NULL)
			{
				return;
			}
			str_end_addr = strstr((char *)str_start_addr, "\r\n");
			str_len = str_end_addr - str_start_addr;
			str_to_hex(str_start_addr, str_len, mainUart.my_data_buf);
			AEP_OTA_Process(mainUart.my_data_buf);
		}
		else

#elif (PROJECT_TYPEDEF == CAT1)
		if (strstr((char *)str_addr, (char *)"LogDown") != NULL)
		{
			if (strstr((char *)str_addr, (char *)"timestampTable") != NULL)
			{
				sprintf((char *)main_tx.my_at_sendbuf,
						"AT+QMTPUB=0,0,0,0,\"LogUp\",{\"log\":\"%d %d %d %d %d %d %d %d\"}\r\n",
						timestampTable[0],
						timestampTable[1],
						timestampTable[2],
						timestampTable[3],
						timestampTable[4],
						timestampTable[5],
						timestampTable[6],
						timestampTable[7]);
				my_usart_send_ERROR(main_tx.my_at_sendbuf);
			}
			else if (strstr((char *)str_addr, (char *)"compareStruct_Last") != NULL)
			{
				sprintf((char *)main_tx.my_at_sendbuf,
						"AT+QMTPUB=0,0,0,0,\"LogUp\",{\"log\":\"led1RestartFlag:%d Last_Hour:%d Last_Minr:%d Last_Sec:%d Lamp:%d %d\"}\r\n",
						led1RestartFlag,
						compareStruct.lastScene.hour,
						compareStruct.lastScene.min,
						compareStruct.lastScene.sec,
						compareStruct.lastScene.ctrl_buf[0].sw,
						compareStruct.lastScene.ctrl_buf[0].light);
				my_usart_send_ERROR(main_tx.my_at_sendbuf);
			}
		}
		else
#endif
		{

			for (i = 0; i < 3; i++) //如果连续收到两包数据，拆包
			{
#ifdef PUBLIC_PROTOCOL
				/*******************************************************************************
				 * 1.若指令包中既包括"630000" 又包括 "730000"，则比较两个首地址，地址小的为起始标识符
				 * 2.若指令包中只包括"630000"，则630000为起始标识符
				 * 3.若指令包中只包括"730000"，则730000为起始标识符
				 * 4.若指令包中既不包括"630000" 又不包括 "730000"，则为错误指令
				 *******************************************************************************/
				if ((strstr((char *)str_addr, (char *)"630000")) && (strstr((char *)str_addr, (char *)"730000")))
				{
					if ((strstr((char *)str_addr, (char *)"630000")) < (strstr((char *)str_addr, (char *)"730000")))
					{
						reg.R.m_73or63 = 1;
					}
					else
					{
						reg.R.m_73or63 = 0;
					}
				}
				else if (strstr((char *)str_addr, (char *)"630000"))
				{
					reg.R.m_73or63 = 1;
				}
				else if (strstr((char *)str_addr, (char *)"730000"))
				{
					reg.R.m_73or63 = 0;
				}
				else
				{
					return;
				}
#else
				if (strstr((char *)str_addr, (char *)"630000") == NULL)
				{
					return;
				}
#endif
#if (PROJECT_TYPEDEF == NB_ONENET)
				str_start_addr = strstr((char *)str_addr, ",");
				ONEent_msgld = atoi(&str_start_addr[1]);
#endif
#ifdef PUBLIC_PROTOCOL
				str_start_addr = reg.R.m_73or63 ? strstr((char *)str_addr, (char *)"630000") : strstr((char *)str_addr, (char *)"730000");
#else
				str_start_addr = strstr((char *)str_addr, (char *)"630000");
#endif

#if (PROJECT_TYPEDEF == NB)
				str_end_addr = strstr((char *)str_start_addr, "\r\n");
#elif (PROJECT_TYPEDEF == NB_ONENET)
				str_end_addr = strstr((char *)str_start_addr, ",");
#elif (PROJECT_TYPEDEF == CAT1)
#if (IP_PUBLIC == ELSE_IP)
				str_end_addr = strstr((char *)str_start_addr, (char *)"\"\r\n");
#else
				str_end_addr = strstr((char *)str_start_addr, (char *)"\"}");
#endif
#endif
				str_addr = str_end_addr;
				if (str_start_addr == NULL || str_end_addr == NULL || str_start_addr > str_end_addr)
				{
					LOGD("num err!\r\n");
#if (PROJECT_TYPEDEF == CAT1)
					sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"error\",{\"error\":\"num err\"}\r\n");
					my_usart_send_ERROR(main_tx.my_at_sendbuf);
#endif
					return; //数据不合法直接返回
				}
				str_len = str_end_addr - str_start_addr;
				if (str_len % 2)
				{
					return; //如果数据为奇数，则数据一定是错的，直接返回
				}

				str_to_hex(str_start_addr, str_len, mainUart.my_data_buf);
				str_len /= 2;
				if (!crc_check(mainUart.my_data_buf, str_len))
				{
					LOGD("ERROR: CRC is error\r\n");
#if (PROJECT_TYPEDEF == CAT1)
					my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, waiting_to_receive, 1, 1000, ACK);
#endif
					my_cmd_deal(DOWMLINK_READ, fun_code_singleWrite, waiting_to_receive, 10, 2000, NO_ACK);
					return; // crc校验
				}
				LOGD("crc ok!\r\n");

				if (reg.RW.ota_flag == OTA_READY)
				{
					device_time.ota_timeout = 0;
				}
#if (PROJECT_TYPEDEF == NB_ONENET)
				my_cmd_deal(AT_QMTOPEN, AT_TX_QMTOPEN, waiting_to_receive, 10, 3000, NO_ACK);
#endif
				data_deal(mainUart.my_data_buf, str_len, WireLess_Uart);
			}
		}
	}
#if (PROJECT_TYPEDEF == CAT1)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTSTAT:") != NULL) //异常返回
	{
		
		my_cmd_init();
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)">") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTPUB:") != NULL && m_uart1.rx_len == 18)
	{
		if (mainUart.my_at_readbuf[15] == '0')
		{
			global_control.ack_ok = 1;
			global_control.error_code = 0;
		}
		else
		{
			global_control.error_code = global_control.cmd_num;
			
		}
	}
#endif

#if (PROJECT_TYPEDEF == NB)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#else

	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+CCLK:") != NULL)
#endif
	{

		StrHead = strstr((char *)mainUart.my_at_readbuf, "+CCLK:");
#if (PROJECT_TYPEDEF == NB)
		StrHead = StrHead + 6;
#elif (PROJECT_TYPEDEF == NB_ONENET)
		StrHead = StrHead + 6;
#else
		StrHead = StrHead + 8;
#endif

		year = (StrHead[0] - '0') * 10 + StrHead[1] - '0';
		month = (StrHead[3] - '0') * 10 + StrHead[4] - '0';
		day = (StrHead[6] - '0') * 10 + StrHead[7] - '0';
		hour = (StrHead[9] - '0') * 10 + StrHead[10] - '0';
		min = (StrHead[12] - '0') * 10 + StrHead[13] - '0';
		sec = (StrHead[15] - '0') * 10 + StrHead[16] - '0';
		global_control.ack_ok = 1;
		global_control.error_code = 0;
#if (PROJECT_TYPEDEF == CAT1)
		timestampCompare[get_cclk_style] = TimeDate_To_TimeStamp(year, month, day, hour, min, sec);
		//LOGD("timestampCompare[%d] = %d\r\n", get_cclk_style,timestampCompare[get_cclk_style]);
		if(Prase_time())
			return;
#endif
		if (year >= 21 && year <= 50) // 30年
		{
			srand(min + sec);					//播种
			device_info.random = rand() % 6000; //生成0~60000的随机数
			LOGD("TIME ON\r\n");

			set_rtc_time(year, month, day, hour, min, sec);
			get_sunset_rise_time();
			if(timestampTable[compareStruct.nextSceneSeek] != TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec))
			{
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			}
		}
	}

#if (PROJECT_TYPEDEF == CAT1)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QNTP:") != NULL)	
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;

    	if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QNTP: 569") != NULL || strstr((char *)mainUart.my_at_readbuf, (char *)"+QNTP: 552") != NULL)
		{
			//get_cclk_another_way();
		}
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+QNTP:");

		StrHead = StrHead + 12;

		year = (StrHead[0] - '0') * 10 + StrHead[1] - '0';
		month = (StrHead[3] - '0') * 10 + StrHead[4] - '0';
		day = (StrHead[6] - '0') * 10 + StrHead[7] - '0';
		hour = (StrHead[9] - '0') * 10 + StrHead[10] - '0';
		min = (StrHead[12] - '0') * 10 + StrHead[13] - '0';
		sec = (StrHead[15] - '0') * 10 + StrHead[16] - '0';

		timestampCompare[get_cclk_style] = TimeDate_To_TimeStamp(year, month, day, hour, min, sec);
    LOGD("timestampCompare[%d] = %d\r\n", get_cclk_style,timestampCompare[get_cclk_style]);
		if(Prase_time())
			return;

		if (year >= 21 && year <= 50) // 30年
		{
			srand(min + sec);					//播种
			device_info.random = rand() % 6000; //生成0~60000的随机数
			//LOGD("device_info.random:%d\r\n", device_info.random);
			LOGD("TIME ON\r\n");
			set_rtc_time(year, month, day, hour, min, sec);
			get_sunset_rise_time();
			if(timestampTable[compareStruct.nextSceneSeek] != TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec))
			{
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			}
		}
	}

	else if (strstr((const char *)mainUart.my_at_readbuf, "+QENG:") != NULL)
	{
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+QENG:");
#if 1	
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;   
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;

		StrHead = Find_Pos(StrHead,',',13);
		StrHead += 1;
		if(StrHead[0] == '-')
		{
			if(StrHead[2] == ',')
			{
				csq = StrHead[1] - '0';
			}
			else if(StrHead[3] == ',')
			{
				csq = (StrHead[1] - '0') * 10 + StrHead[2] - '0';
			}
			else if(StrHead[4] == ',')
			{
				csq = (StrHead[1] - '0') * 100 + (StrHead[2] - '0') * 10 + (StrHead[3] - '0') ;
			}
			device_info.csq = -csq;
			reg.R.csq = -csq;
		}
		else
		{
			  if(StrHead[1] == ',')
				{
					csq = StrHead[0] - '0';
				}
				else if(StrHead[2] == ',')
				{
					csq = (StrHead[0] - '0') * 10 + StrHead[1] - '0';
				}
				else if(StrHead[3] == ',')
				{
					csq = (StrHead[0] - '0') * 100 + (StrHead[1] - '0') * 10 + (StrHead[2] - '0') ;
				}
				device_info.csq = csq;
				reg.R.csq = csq;
		}
		//reg.R.csq  = atoi(StrHead);
		
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
//		StrHead = strstr((char *)StrHead, ",") + 1;
		StrHead = Find_Pos(StrHead,',',3);
		StrHead += 1;
		if(StrHead[0] == '-')
		{
			if(StrHead[2] == ',')
			{
				csq = StrHead[1] - '0';
			}
			else if(StrHead[3] == ',')
			{
				csq = (StrHead[1] - '0') * 10 + StrHead[2] - '0';
			}
			else if(StrHead[4] == ',')
			{
				csq = (StrHead[1] - '0') * 100 + (StrHead[2] - '0') * 10 + (StrHead[3] - '0') ;
			}
			reg.R.sinr = -csq;
		}
		else
		{
			if(StrHead[1] == ',')
			{
				csq = StrHead[0] - '0';
			}
			else if(StrHead[2] == ',')
			{
				csq = (StrHead[0] - '0') * 10 + StrHead[1] - '0';
			}
			else if(StrHead[3] == ',')
			{
				csq = (StrHead[0] - '0') * 100 + (StrHead[1] - '0') * 10 + (StrHead[2] - '0') ;
			}
			reg.R.sinr = csq;
		}
#else
		StrHead = strstr((char *)mainUart.my_at_readbuf, "+QENG:");
		StrHead = Find_Pos(StrHead,',',13);
		StrHead += 1;
		reg.R.csq  = atoi(StrHead);
		StrHead = Find_Pos(StrHead,',',3);
		StrHead += 1;
		reg.R.sinr = atoi(StrHead);
		///reg.R.sinr = (signal2<<8) + signal1;
#endif
		if((reg.R.csq > -95) && (reg.R.sinr > 16))
		{
			/**/
			reg.R.signal_quality =  3 << 8;
		}
		else if((reg.R.csq > -105) && (reg.R.sinr > 11))
		{
			reg.R.signal_quality =  2 << 8;
		}
		else if((reg.R.csq > -115) && (reg.R.sinr > 3))
		{
			reg.R.signal_quality =  1 << 8;
		}
		else
		{
			reg.R.signal_quality = 0 << 8;
		}
			global_control.ack_ok = 1;
			global_control.error_code = 0;
			reg.R.wireless_run_state.csq = 1;
	}
#endif

#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
	else if (strstr((const char *)mainUart.my_at_readbuf, "+CSQ:") != NULL && m_uart1.rx_len == 20)
#elif (PROJECT_TYPEDEF == CAT1)
	else if (strstr((const char *)mainUart.my_at_readbuf, "+CSQ:") != NULL && m_uart1.rx_len == 21)
#endif
	{
#if (PROJECT_TYPEDEF == NB)
		csq = (mainUart.my_at_readbuf[7] - 0x30) * 10 + (mainUart.my_at_readbuf[8] - 0x30);
#elif (PROJECT_TYPEDEF == NB_ONENET)
		csq = (mainUart.my_at_readbuf[7] - 0x30) * 10 + (mainUart.my_at_readbuf[8] - 0x30);
#elif (PROJECT_TYPEDEF == CAT1)
		csq = (mainUart.my_at_readbuf[8] - 0x30) * 10 + (mainUart.my_at_readbuf[9] - 0x30);
#endif

#if (PROJECT_TYPEDEF == NB)
		if (csq != 99 && csq >= 8)
#else
		if (csq != 99 && csq != 0)
#endif
		{
			device_info.csq = csq;
			reg.R.csq = csq;
			global_control.ack_ok = 1;
			global_control.error_code = 0;
			reg.R.wireless_run_state.csq = 1;

			if(reg.R.csq <= 10)
			{
				/**/
				reg.R.signal_quality =  0 << 8;
			}
			else if(reg.R.csq <= 18)
			{
				/**/
				reg.R.signal_quality =  1 << 8;
			}
			else if(reg.R.csq < 28)
			{
				/**/
				reg.R.signal_quality =  2 << 8;
			}
			else if(reg.R.csq > 28)
			{
				/**/
				reg.R.signal_quality =  3 << 8;
			}
		}
#if (PROJECT_TYPEDEF == NB)
		else if (NULL != NB_return_error_typedef())
		{
		}
#endif
		else
		{
			global_control.error_code = global_control.cmd_num;
			reg.R.wireless_run_state.csq = 0;
		}
	}
#if (PROJECT_TYPEDEF == CAT1)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		return;
	}
#endif
#if (PROJECT_TYPEDEF == NB)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QLWEVTIND:3") != NULL)
	{
		my_cmd_init();
	}

	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		

		if (start_ota_flag == TRUE)
		{
			start_ota_flag = FALSE;
			//ota_before_ready();
			//if(reg.RW.ota_flag != OTA_READY)
			{
				mainUart.my_data_buf[3] = 21;
				my_cmd_deal(DOWMLINK_READ, APP_PACKsend_request_ota_pack_21, waiting_to_receive, 1, 2000, ACK);
			}
			
		}
	}
	else if (NULL != NB_return_error_typedef())
	{
	}
#elif (PROJECT_TYPEDEF == NB_ONENET)
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"OK") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT: 0,11") != NULL)
	{
		global_control.ack_ok = 1;
		global_control.error_code = 0;
		// my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT: 0,12") != NULL)
	{
		// global_control.ack_ok = 1;
		// global_control.error_code = 0;
		my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT: 0,13") != NULL)
	{
		// global_control.ack_ok = 1;
		// global_control.error_code = 0;
		my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT: 0,14") != NULL)
	{
		// global_control.ack_ok = 1;
		// global_control.error_code = 0;
		my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT: 0,20") != NULL)
	{
		// global_control.ack_ok = 1;
		// global_control.error_code = 0;
		my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLEVENT:") != NULL)
	{
		// global_control.ack_ok = 1;
		// global_control.error_code = 0;
		return;
	}
	else if (strstr((char *)mainUart.my_at_readbuf, (char *)"+MIPLDISCOVER:") != NULL) //数据接收
	{
		char *p = NULL;
		p = strstr((char *)mainUart.my_at_readbuf, (char *)",");
		ONEent_msgld = atoi(&p[1]);

		device_info.mqtt_state = NB_LWM2M_OK;
		sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
		device_info.QmtSubFlag = 1;

		device_time.first_data = 0;
		device_time.lamp_data_report_time = 0;
		device_time.else_data_repota_time =  ((u32)reg.W.data_tx_time * 100) + device_info.random - 6000;
		device_time.at_time = 30000 * 12 - 6000;

		// my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 2000, NO_ACK); //查询卡的状态，同时转为接收模式等待接
		my_cmd_deal(AT_QMTCFG_3, AT_TX_QMTCFG_3, waiting_to_receive, 10, 3000, ACK);
		global_control.ack_ok = 1;
		global_control.error_code = 0;
	}
#endif
	else
	{
 		global_control.error_code = global_control.cmd_num;
		if (strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTPUBEX:") != NULL )
		{
			StrHead = strstr((char *)mainUart.my_at_readbuf, (char *)"+QMTPUBEX:");
			if (StrHead[15] == '0')
			{
				global_control.ack_ok = 1;
				global_control.error_code = 0;
			}
			else
			{
				global_control.error_code = global_control.cmd_num;
				
			}
		}
	//	LOGD("/********************************else data:%s\r\n", mainUart.my_at_readbuf);
	}
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/


void ota_before_ready(void)
{
	if (reg.RW.ota_flag == NONE_OTA)
	{
#if (PROJECT_TYPEDEF == NB_ONENET)
		sprintf((char *)main_tx.my_at_sendbuf, "AT+MIPLWRITERSP=0,%d,2\r\n", ONEent_msgld);
		my_usart_send_data(main_tx.my_at_sendbuf, strlen((char *)main_tx.my_at_sendbuf));
#endif
		reg.RW.ota_flag = IS_OTA;
		reg.R.run_state = 1;
		reg.R.upgrade_mark = 1;
		reg.RW.year_mon = (rtc_time.year << 8) + rtc_time.month;
		reg.RW.day_hour = (rtc_time.date << 8) + rtc_time.hour;
		reg.RW.min_sec = (rtc_time.min << 8) + rtc_time.sec;

		data_flash_write();
		IAP_IAP_Jump();
	}
	else
	{
		// FlashErase(FLASH_OTA_ADDR, 8);
		FlashErase(FLASH_OTA_ADDR, 512);
		//LOGD("ERASE FLASH!");
		reg.RW.ota_flag = OTA_READY;
		reg.R.upgrade_mark = 1;
#if (PROJECT_TYPEDEF == NB)
		//my_cmd_deal(DOWMLINK_READ, APP_PACKsend_request_ota_pack_21, waiting_to_receive, 1, 2000, ACK);
#else
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, waiting_to_receive, 1, 1000, ACK);
#endif
		my_cmd_deal(AT_QMTPUB_2018, AT_TX_QMTPUB_2018, waiting_to_receive, 1, 2000, ACK);
#endif

		//return 1;
	}
}
#if (PROJECT_TYPEDEF != NB)
u8 ota_special_handling()
{
	/*  升级相关做特殊处理 */
#if (PROJECT_TYPEDEF == NB_ONENET)
	static u8 ota_tingle = 0;
#endif
	if (protocol.reg_addr[0] == 0x1007)
	{
		/*升级任务的第一包数据，下发一个随机数*/
		memset(&device_time, 0, sizeof(device_time));
#if (PROJECT_TYPEDEF == NB_ONENET)
		ota_package_addr = 0x26FF;
#endif
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, AT_RX_UPLINK, 1, 1000, ACK);
#endif
		my_cmd_deal(AT_QMTPUB_1007, AT_TX_QMTPUB_1007, waiting_to_receive, 1, 2000, ACK);
#if (PROJECT_TYPEDEF == NB_ONENET)
		ota_tingle = 1;
#endif
		return 1;
	}
	else if (protocol.reg_addr[0] == 0x2018)
	{
		reg.W.ota_sum_len = 0;
		ota_checkBuf = 0x26ff;
		memset(&device_time, 0, sizeof(device_time));
		//此处这样设计，是为了让其在BootLoader中升级时直接升级而无需跳转
#if (PROJECT_TYPEDEF == NB_ONENET)
		if (1 == ota_tingle)
#endif
		{
#if (PROJECT_TYPEDEF == NB_ONENET)
			ota_tingle = 0;
#endif
			//ota_before_ready();
		}
	}
	else if (protocol.reg_addr[0] == 0x1008)
	{ /*升级任务的最后一包数据，下发升级完成，此时检验包长对不对*/
		if (protocol.data[0] == OTA_PACKAGE_TX_OK)
		{
			otaState_check(1);
			if (reg.W.software_len == reg.W.ota_sum_len)
			{
				reg.W.software_len = reg.W.software_len - 16;
				if (MD5_Check(FLASH_OTA_ADDR, reg.W.software_len)) // MD5计算
				{
					LOGD("MD5 ok pk\r\n");
					reg.RW.ota_flag = OTA_PACKAGE_RX_OK;
				}
				else
				{
					LOGD("MD5 fail");
					reg.RW.ota_flag = OTA_FAIL;
#if (PROJECT_TYPEDEF == CAT1)
					sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"error\",{\"error\":\"MD5 fail\"}\r\n");
					my_usart_send_ERROR(main_tx.my_at_sendbuf);
#endif
				}
			}
			else
			{
				//LOGD("ERROR:reg.W.ota_sum_len:%d", reg.W.ota_sum_len);
				reg.RW.ota_flag = OTA_FAIL;
#if (PROJECT_TYPEDEF == CAT1)
				sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"error\",{\"error\":\"Ota_Sum_Len Error:%d\"}\r\n", reg.W.ota_sum_len);
				my_usart_send_ERROR(main_tx.my_at_sendbuf);
#endif
			}
			protocol.data[0] = reg.RW.ota_flag;
			if (reg.RW.ota_flag == OTA_FAIL)
			{
				protocol.reg_num = 2;
				protocol.reg_addr[0] = 0x1008;
				protocol.reg_len[0] = 1;
				protocol.reg_addr[1] = 0x1007;
				protocol.reg_len[1] = 1;
#if (PROJECT_TYPEDEF == CAT1)
				my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
				LOGD("test09:AT_TX_UPLINK10\r\n");
#endif
				my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
				return 1;
			}
		}
		if (protocol.data[0] == OTA_START) //升级完成
		{
#if (PROJECT_TYPEDEF == CAT1)
			fun_code_singleWrite();
			my_usart_send_data(main_tx.my_at_sendbuf, strlen((char *)main_tx.my_at_sendbuf));
			reg.RW.ota_flag = NONE_OTA;
			data_flash_write();
			LOGD("IAP_APP_Jump:1");
			//NVIC_SystemReset(); //复位
			IAP_APP_Jump();
#else
			fun_code_singleWrite();
			my_usart_send_data(main_tx.my_at_sendbuf, strlen((char *)main_tx.my_at_sendbuf));
			reg.RW.ota_flag = OTA_START;

#endif
		}
	}
	return 0;
}
#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
extern void my_pc_send_data(uint8_t *pbuf, uint16_t len);
void data_deal(uint8_t *deal_buf, uint16_t deal_data_len, Uart_Com uart_tx)
{
	uint8_t i;
	protocol.fun_code = deal_buf[5]; //功能码
#ifdef PUBLIC_PROTOCOL
	if(reg.R.m_73or63)
	{
		if((protocol.fun_code != 0x03) && ((protocol.fun_code != 0x06)) && (protocol.fun_code != 0x10))
		{
			return ;
		}
		else{
			/**do nothing**/
		}
	}
	else{
		if((protocol.fun_code != 0x31) && ((protocol.fun_code != 0x61)) && (protocol.fun_code != 0x11))
		{
			return ;
		}
		else{
			/**do nothing**/
		}
	} 
#endif

	switch (protocol.fun_code)
	{
#ifdef PUBLIC_PROTOCOL
	case 0x03:
	case 0x31:
#else
	case 0x03:
#endif
		deal_buf += 6;
		protocol.reg_addr[0] = pCharToU16(deal_buf);
		deal_buf += 2;
		protocol.reg_len[0] = *(deal_buf++);

		/*读数据处理在fun_code_read函数中*/
		if (WireLess_Uart == uart_tx)
		{
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK03, waiting_to_receive, 1, 1000, ACK);
#endif
			my_cmd_deal(DOWMLINK_READ, fun_code_read, waiting_to_receive, 1, 2000, ACK);
		}
		else
		{
			pc_uart_return(fun_code_read);
		}
		break;
#ifdef PUBLIC_PROTOCOL
	case 0x06:
	case 0x61:
#else
	case 0x06:
#endif

		deal_buf += 6;
		protocol.reg_addr[0] = pCharToU16(deal_buf);
		deal_buf += 2;
		protocol.reg_len[0] = 1;
		protocol.data_buf[0] = deal_buf;
		protocol.data[0] = pCharToU16(deal_buf);
		deal_buf += 2;

		/*将数据写入到内存中，同时对地址判断是场景策略包还是升级包还是其他正常包*/
		protocol_write(protocol.reg_addr[0], protocol.data_buf[0], protocol.reg_len[0]);
		
		if (WireLess_Uart == uart_tx)
		{
#if (PROJECT_TYPEDEF != NB)
			if (ota_special_handling())
			{
				return;
			}
#endif
		}
		/*将回包命令加入队列*/
		if (WireLess_Uart == uart_tx)
		{
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, AT_RX_UPLINK, 1, 1000, ACK);
#endif
			my_cmd_deal(DOWMLINK_READ, fun_code_singleWrite, waiting_to_receive, 1, 2000, ACK);
		}	
		else
		{
			pc_uart_return(fun_code_singleWrite);
		}
		if ((protocol.reg_addr[0] == 0x2014) ||
			(protocol.reg_addr[0] == 0x2015) ||
			(protocol.reg_addr[0] == 0x2016) ||
			(protocol.reg_addr[0] == 0x2017))
		{
			get_sunset_rise_time();
			if(timestampTable[compareStruct.nextSceneSeek] != TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec))
			{
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			}
		}
		/*保存数据*/
		data_flash_write(); //保存一次参数
		break;
#ifdef PUBLIC_PROTOCOL
	case 0x10:
	case 0x11:
#else
	case 0x10:
#endif
		protocol.reg_num = deal_buf[6];
		deal_buf += 7;
		if (protocol.reg_num > 3)
		{
		//	LOGD("ERROR:protocol.reg_num is error:%d", protocol.reg_num);
#if (PROJECT_TYPEDEF == CAT1)
			sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"error\",{\"error\":\"protocol.reg_num error:%d\"}\r\n", protocol.reg_num);
			my_usart_send_ERROR(main_tx.my_at_sendbuf);
#endif
			return;
		}
		for (i = 0; i < protocol.reg_num; i++) /*多个寄存器处理*/
		{
			protocol.reg_addr[i] = pCharToU16(deal_buf);
			deal_buf += 2;
			protocol.reg_len[i] = *(deal_buf++);
			protocol.data_buf[i] = deal_buf;
#if (PROJECT_TYPEDEF == NB_ONENET)
			if (protocol.reg_addr[i] >= 0x2700 && protocol.reg_addr[i] < 0x3000 && (reg.RW.ota_flag == OTA_READY)) //固件升级
			{
				if ((ota_package_addr + 1) == protocol.reg_addr[i])
				{
					protocol_write(protocol.reg_addr[i], protocol.data_buf[i], protocol.reg_len[i]);
				}
				else
				{
					/*do nothing*/
				}
			}
			else
			{
				/*将数据写入到内存中，同时对地址判断是场景策略包还是升级包还是其他正常包*/
				protocol_write(protocol.reg_addr[i], protocol.data_buf[i], protocol.reg_len[i]);
			}
#else
			protocol_write(protocol.reg_addr[i], protocol.data_buf[i], protocol.reg_len[i]);
#endif
			deal_buf = deal_buf + protocol.reg_len[i] * 2;
		}
		if (protocol.reg_addr[0] >= 0x2700)
		{
			//如果是升级包，回包的时候要回0x1007的内容
			if (WireLess_Uart == uart_tx)
			{
#if (PROJECT_TYPEDEF == NB_ONENET)
				if ((ota_package_addr + 1) == protocol.reg_addr[0] && (reg.RW.ota_flag == OTA_READY))
				{
					ota_package_addr = protocol.reg_addr[0];
					my_cmd_deal(DOWMLINK_READ, fun_code_singleWrite, waiting_to_receive, 10, 2000, NO_ACK);
				}
				else if(reg.RW.ota_flag == OTA_FILE_ERROR)
				{
					protocol.reg_num = 1;
					protocol.reg_addr[0] = 0x1008;
					protocol.reg_len[0] = 1;
					my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
				}
				else
				{
					/*do nothing*/
				}
#else
				if (reg.RW.ota_flag == OTA_READY)
				{
#if (PROJECT_TYPEDEF == CAT1)
					my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, AT_RX_UPLINK, 1, 1000, ACK);
#endif
					my_cmd_deal(DOWMLINK_READ, fun_code_singleWrite, waiting_to_receive, 10, 2000, NO_ACK);
				}
					
				else if(reg.RW.ota_flag == OTA_FILE_ERROR)
				{
					protocol.reg_num = 1;
					protocol.reg_addr[0] = 0x1008;
					protocol.reg_len[0] = 1;
#if (PROJECT_TYPEDEF == CAT1)
					my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
					LOGD("test10:AT_TX_UPLINK10\r\n");
#endif
					my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
				}
#endif
			}
			else
			{
				pc_uart_return(fun_code_singleWrite);
			}
			memset(&device_time, 0, sizeof(device_time));
		}
		else
		{
			if (WireLess_Uart == uart_tx)
			{
#if (PROJECT_TYPEDEF == CAT1)
				my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
				LOGD("test12:AT_TX_UPLINK10\r\n");
#endif
				my_cmd_deal(DOWMLINK_READ, fun_code_write, waiting_to_receive, 1, 2000, ACK);
			}
			else
			{
				pc_uart_return(fun_code_write);
			}
		}
		if ((protocol.reg_addr[0] == 0x2014) ||
			(protocol.reg_addr[0] == 0x2015) ||
			(protocol.reg_addr[0] == 0x2016) ||
			(protocol.reg_addr[0] == 0x2017))
		{
			get_sunset_rise_time();
			if(timestampTable[compareStruct.nextSceneSeek] != TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec))
        	{
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			}
		}
		data_flash_write(); //保存一次参数
		break;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
